package edu.whu.irlab.woscrawl.util;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * Created by zhongwei on 2016/12/29.
 */
public class HttpDownloader {

    private static HttpClient client = null;

    static {
        HttpClientBuilder builder = HttpClients.custom();
        builder.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36");
        client = builder.build();
    }

    public static String download(String url) {
        HttpGet get = new HttpGet(url);
        HttpResponse response = null;
        String ret = "";
        try {
            response = client.execute(get);
            ret = EntityUtils.toString(response.getEntity());
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            if (response != null) EntityUtils.consumeQuietly(response.getEntity());
        }
        return ret;
    }
}
